<?php
/**
 * @author Sunny
 * @date 2023/5/31
 * @time 14:06 下午
 * @focus love & life
 */


namespace app\api\controller;

use app\auto\model\Method;
use app\common\lib\Sensitive;
use app\common\model\ArticleClass;
use app\common\model\MemberPointsLog;
use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use think\db\Raw;
use think\Exception;
use think\facade\Db;
use think\response\Json;

/**
 * Class ArticleTitleTask
 * @package app\api\controller
 *
 * @Apidoc\Title("批量生成标题和文章")
 * @Apidoc\Group("api")
 */
class ArticleTitleTask extends Auth
{


    /**
     * addTitleTask
     * @return Json|void
     *
     * NotHeaders
     * @Apidoc\Title("批量生成标题任务")
     * @Apidoc\Url("/api/ArticleTitle/addTitleTask")
     * @Apidoc\Method("post")
     * @Param("keywords",type="varchar",require=true,desc="关键词")
     * @Param("article_class_id",type="int",require=true,desc="分类id")
     * @Param("lang",type="varchar",require=true,desc="生成语言")
     * @Param("checkType",type="int",require=true,desc="生成方式")
     * @Param("num",type="int",require=true,desc="生成数量")
     */
    public function addTitleTask()
    {
        $member_id = $this->request->uid;
        $keywords = input('keywords','', 'trim');
        $article_class_id = input('article_class_id', '', 'intval');
        $lang = input('lang', '中文', 'trim');
        $check_type = input('check_type', 0, 'intval');
        $forbiddenwords_id = input('forbiddenwords_id', 0, 'intval');
        $num = input('num',500, 'intval');
        $length = input('length',500, 'intval');
        $remark = input('remark', '', 'trim');
        if(!$keywords) {
            return error('关键词不能为空', []);
        }
        //敏感词过滤
        $res = Sensitive::has($keywords);
        if($res) {
            //return error('不能包含敏感词：'. Sensitive::get($keywords));
            return error('不能包含敏感词');
        }
        //
        if(!$article_class_id) {
            return error('请选择分类', []);
        }
        // 积分验证
        $member = \app\common\model\Member::find($member_id);
        $cost_points = $num * config('user.point_per_article');
        $member_point_num = $member['point_num'];
        if ($member_point_num > $cost_points ) {
            \app\common\model\Member::update([
                'id' => $member_id,
                'point_num' => $member_point_num - $cost_points
            ]);
        } else {
            return error('积分不足, 请充值积分！',['action' => 'recharge']);
        }
        //积分记录
        $point_log = MemberPointsLog::create([
            'member_id' => $member_id,
            'num' => -1 * $cost_points,
            'end_num' => $member_point_num - $cost_points,
            'remarks' => "批量文章创作"
        ]);
        $point_log->save();
        //
        \app\common\model\ArticleTitleTask::create([
            'keywords' => $keywords,
            'article_class_id' => $article_class_id,
            'forbiddenwords_id' => $forbiddenwords_id,
            'lang' => $lang,
            'check_type' => $check_type,
            'member_id' => $member_id,
            'num' => $num,
            'length'=>$length,
            'remark'=>$remark
        ]);

        return success('添加成功',);
    } 
    /**
     * list
     * @Apidoc\Title("任务列表")
     */
    public function list()
    {
        $list_rows = input('list_rows', 10, 'intval');
        $where[] = ['member_id', '=', $this->request->uid];

        $res = \app\common\model\ArticleTitleTask::with('articleClass')
            ->order("id desc")
            ->where($where)->paginate($list_rows);
        $res = $res->toArray();

        return success('ok', $res);
    }
    /**
     * articleList
     * @Apidoc\Title("任务列表")
     * @Apidoc\Url("/api/ArticleTitleTask/articleList")
     * @Apidoc\Method("get")
     * @Param("id",type="Int",require=true,desc="任务id")
     */
    public function articleList()
    {
        $id = input('id', 0, 'intval');
        if(!$id){
            return error('缺少id参数！',['action' => 'recharge']);
        }
        $where[] = ['member_id', '=', $this->request->uid];
        $where[] = ['article_title_task_id', '=', $id];

        $res = \app\common\model\Article::with('articleClass')
            ->order("id desc")
            ->where($where)->select();
        $res = $res->toArray();
        return success('ok', $res);
    }  
    /**
     * getContent
     * @Apidoc\Title("根据文章分类id查询标题任务详情")
     * @Apidoc\Query("article_class_id", type="int", desc="文章分类id")
     */
    public function getContent(): Json
    {
        $article_class_id = input('article_class_id');
        $content = \app\common\model\ArticleTitleTask::where('article_class_id', $article_class_id)->order("id desc")->find();
        return success('ok',$content);
    }    
}
